var api = require('../../utils/api.js');
var app = getApp();
Page({
    data: {
        tips:1,
        tophe:0,
        code:'',
        openid:'',
        banner:[],
        address:'',
        key:'',
        hospital:[]
    },
    currentPageNumber: 1,
    onLoad() {
        var that=this;

        this.getbanner();

        wx.getFuzzyLocation({
          type: "wgs84",
          success: function (t) {
            console.log("--******--", t)
            api.post({
                url: 'wxapp/getAddress',
                data: {latitude:t.latitude,longitude:t.longitude},
                success: data => {
                    console.log(data.data)

                    if(data.code=='0'){
                        wx.showToast({
                          title: data.msg,
                          icon:'none'
                        })
                    }

                    wx.setStorageSync('address', data.data.name)
                    that.setData({
                        address:wx.getStorageSync('address')
                    })

                    that.gethosptilbycity();
                }
            })
          },
          fail: function (t) {
            console.log("--******--", t);
          },
        })

    },
    onShow() {
        var that=this;
        let jn = wx.getMenuButtonBoundingClientRect();
        console.log(jn);
        var tophe=jn.top+jn.height;
        var over =tophe+230 ;
        var add=wx.getStorageSync('address')
        this.setData({
            address:add,
            tophe:tophe,
            over:over+ 'px'
        })
         
        wx.login({
            success: function (ret) {
                console.log(ret);
                wx.getUserInfo({
                    success: function () {
                        api.post({
                            url: 'wxapp/getopenid',
                            data: {'code':ret.code},
                            success: data => {
                                that.setData({
                                    openid:data.data.openid,
                                    userinfo:data.data.userinfo,
                                    token:data.data.userinfo.token
                                })

                                wx.setStorageSync('token', data.data.userinfo.token)
                                wx.setStorageSync('userinfo', data.data.userinfo)

                            }
                        })
                    }
                })
            }
        })
        
      

        this.gethosptilbycity();
     
    },

    getalllbyky(ret){
        this.setData({
            key:ret.detail.value
        })

        this.gethosptilbycity();
    },
    hidetip(){
        this.setData({
            tips:0
        })

    },

    // 医院信息
    gethosptilbycity(){
        var that=this;
        var key=this.data.key;
        var city=this.data.address;
        that.setData({
            hospital:[]
        })

        api.post({
            url: 'hospital/gethospital',
            data: {key:key,city:city},
            success: data => {
                console.log(data.data)
                that.setData({
                    hospital:data.data
                })

            }
        })

    },

    getbanner(){
        var that=this;
        api.post({
            url: 'index/getSlider',
            data: {},
            success: data => {
                console.log(data.data)
                that.setData({
                    banner:data.data
                })

            }
        })
      
    },
    
    /**
     * 上拉刷新
     */
    pullUpLoad() {
        

    },

    //详情
    goindexinfo(ret){
        console.log(ret);
        var id=ret.currentTarget.dataset.id;

        wx.navigateTo({
          url: '/pages/index/info?id='+id
        })

        //pages/index/info
    },
    
    onShareAppMessage() {
        return {
            title: '好玩的,都在这儿~~',
            path: '/pages/index/index'
        }
    },
    seelogs(){
        wx.navigateTo({
          url: '/pages/comment/comment',
        })
    },
    gootherwxapp(){
        wx.navigateToMiniProgram({
            appId: 'wx2e3255c24f373c8b',
            path: '/pages/index/index',
            envVersion: 'release', 
          });
    },
    getaddress(){
        wx.navigateTo({
          url: '/pages/index/address',
        })
    },


    bindaddress(ret){
       console.log(ret);

       var add=ret.detail.value;

       this.setData({
           address:add[1]
       })

       wx.setStorageSync('address', add[1])

       this.gethosptilbycity();
    }

});
